package com.itheima.service.Impl.company;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.Company;
import com.itheima.mapper.store.company.CompanyMapper;
import com.itheima.service.CompanyService;
import com.itheima.utils.IDutils;
import com.itheima.utils.MapperUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class ComServiceImpl implements CompanyService {


    /**
     * 查询全部公司用户
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public List<Company> findAll(String page, String pageSize) {
        SqlSession session = MapperUtil.getSession();
        CompanyMapper mapper = session.getMapper(CompanyMapper.class);

        // 设置默认分页参数
        int page11=1;
        int pageSize22=10;
        // 判断传入的对象是否为空
        if(page!=null){
            page11=Integer.parseInt(page);
        }
        if (pageSize!=null){
            pageSize22=Integer.parseInt(pageSize);
        }
        // 分页助手启动
        PageHelper.startPage(page11,pageSize22);
        // 调用查询方法
        List<Company> companyList= mapper.findAll();
        MapperUtil.close(session);
        return companyList;
    }

    @Override
    public void save(Company company) {
        SqlSession session = MapperUtil.getSession();
        CompanyMapper mapper = session.getMapper(CompanyMapper.class);

        company.setId(IDutils.getUUId());
        mapper.save(company);
        session.commit();
        MapperUtil.close(session);
    }

    @Override
    public Company findById(String id) {
        SqlSession session = MapperUtil.getSession();
        CompanyMapper mapper = session.getMapper(CompanyMapper.class);
        Company company=   mapper.findById(id);
        MapperUtil.close(session);
        return company;
    }

    @Override
    public void update(Company company) {
        SqlSession session = MapperUtil.getSession();
        CompanyMapper mapper = session.getMapper(CompanyMapper.class);
        mapper.update(company);
        session.commit();
        MapperUtil.close(session);
    }

    @Override
    public void delete(String ids) {
        SqlSession session = MapperUtil.getSession();
        CompanyMapper mapper = session.getMapper(CompanyMapper.class);
        String[] id = ids.split(",");
        mapper.delete(id);
        session.commit();
        MapperUtil.close(session);
    }
}
